package com.iuie.ns.web.tags;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.iuie.ns.web.beans.DropItemVO;
import com.iuie.ns.web.html.HTMLTag;

/**
 * @desc 常量drop系列，dropList
 * @createTime 2017年7月20日 下午2:33:00
 * @author iuie
 */
public class DropListTag extends SimpleTagSupport {
    
    private String id;
    
    private Map<String, DropItemVO> dropItems;
    
    private String emptyText;
    
    private String type;
    
    @Override
    public void doTag() throws JspException, IOException {
        getJspBody().invoke(null);
        
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> droplist = new HashMap<String, Object>();
        droplist.put("id", id);
        droplist.put("emptyText", emptyText);
        droplist.put("dropItems", dropItems);
        droplist.put("type", "drop_constant");
        
        String jsObj = mapper.writeValueAsString(droplist);
        JspWriter writter = getJspContext().getOut();
        String js = "var " + id + " = " + jsObj;
        
        HTMLTag tag = new HTMLTag("script");
        tag.addAttribute("type", "text/javascript");
        tag.setTextNode(js);
        
        writter.write(tag.toString());
    }
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public Map<String, DropItemVO> getDropItems() {
        if (this.dropItems == null ) {
            this.dropItems = new HashMap<>();
        }
        return dropItems;
    }
    public void setDropItems(Map<String, DropItemVO> dropItems) {
        this.dropItems = dropItems;
    }
    public String getEmptyText() {
        return emptyText;
    }
    public void setEmptyText(String emptyText) {
        this.emptyText = emptyText;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
}
